/*
 * 3_临时文件操作.c
 * 创建临时文件，修改文件，保存临时文件并覆盖原文件
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct
{char name[20]; char pwd[7];}User;

int main(void)
{
	FILE* temp=tmpfile();
	FILE* f=fopen("manage.txt","r"); 
	User users;   
	while(fscanf(f,"%s %s",users.name,users.pwd)>0){
		if(!strcmp(users.name, "user1")){
		strncpy(users.name,"name\0change",20);
		strncpy(users.pwd,"99",7);
		}
		fwrite(&users,sizeof(User),1,temp);
	}
	fclose(f);
	
	FILE* fp=fopen("manage.txt","w");
	while(fread(&users,sizeof(User),1,temp)){
		printf("%s %s\n",users.name,users.pwd);
		fprintf(fp, "%s %s\n",users.name,users.pwd);
	}
	//fputc('\0',fp);//加入'\0'后面就再加不了东西，加了的不会再显示，也无法追加
	fclose(fp);
	FILE* fpp=fopen("manage.txt","a");
	fputc('a',fpp);fputc('a',fpp);fclose(fpp);
}

